import { _decorator, Component, instantiate, Label, Node, Sprite, SpriteFrame } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Hero, Stone } from '../entity/Hero';
const { ccclass, property } = _decorator;

@ccclass('StoneTip')
export class StoneTip extends Component {
    @property(Node)
    mask:Node
    @property(Node)
    close:Node
    @property(Node)
    content:Node
    @property(Node)
    confirm:Node
    @property(Node)
    item:Node
    @property(SpriteFrame)
    upgradeStone:SpriteFrame

    start() {
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.confirm.on(Node.EventType.TOUCH_END, this.onClose, this)
    }

    async setData(list:Stone[]){
        this.item.removeFromParent()
        this.content.destroyAllChildren()
        for(let s of list){
            let node = instantiate(this.item)
            let cover = node.getChildByName('Cover').getComponent(Sprite)
            let stoneName = node.getChildByName('StoneName').getComponent(Label)

            stoneName.string = `${s.name}\nx${s.count}`

            if(s.heroId){
                let hero = new Hero()
                hero.id = s.heroId
                cover.spriteFrame = await hero.getHeadImg()
            } else {
                cover.spriteFrame = this.upgradeStone
            }

            this.content.addChild(node)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.StoneTip)
    }
}


